let express=require('express')
let Router=express.Router()
// 导出Router
module.exports=Router
let query=require('./db')
// 收藏与取消文章
// 参数：falg,user_id,article_id
Router.post('/isShouChang',async(req,res)=>{
    let {flag,article_id}=req.body
    let {user_id}=req.session.info
    let sql=''
    if(flag=='true'){
        sql=`INSERT INTO shoucang VALUES (${null},${user_id},${article_id})`
    }else{
        sql=`DELETE FROM shoucang WHERE user_id =${user_id} and article_id=${article_id}`
    }
    try {
        console.log(`INSERT INTO shoucang VALUES (${null},${user_id},${article_id})`);
        let data=await query(sql)
        res.send({
            code:200,
            msg:'修改成功',
        })
    } catch (error) {
        res.send({
            code:400,
            msg:'数据库操作错误',
            flag,article_id,user_id
        })
    }
    

})
// 获取用户收藏的文章
Router.get('/shouCang',async(req,res)=>{
    let {user_id}=req.session.info
    try {
        console.log(`select * from shouCang inner join wenZhang on  shouCang.article_id=article_id where shouCang.user_id=${user_id}`);
        let data=await query(`select * from shouCang inner join wenZhang on  shouCang.article_id=wenZhang.article_id where shouCang.user_id=${user_id}`)
        res.send({ 
            code:200,
            msg:'查询成功a',
            data
        })
    } catch (error) {
        res.send({
            code:400,
            msg:'数据库操作错误',
        })
    }
})